<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/6/5
 * Time: 17:10
 */

namespace app\common\model;


use think\Validate;

class SignupForm extends Validate
{
    public $uname;
    public $pwd;
    public $phone;
    //自动验证规则
    protected $rule = [
        'uname' => 'require|/^[A-Za-z\x{4e00}-\x{9fa5}_]{1}[A-Za-z0-9\x{4e00}-\x{9fa5}_]{1,19}$/u|unique:user',
        'pwd' => 'require|length:6,20',
        'phone' => '/^1[34578]\d{9}$/|unique:user',
    ];

    /**
     * 注册
     * @return User|null
     */
    public function signup(){
        $data = [
            'uname'=>$this->uname,
            'pwd'=>$this->pwd,
            'phone'=>$this->phone
        ];
        if ($this->check($data)) {
            $user = new User();
            $user->uname = $this->uname;
            $user->phone = $this->phone;
            $user->generateLoginSalt();
            $user->setPassword($this->pwd);
            $user->save();
            return $user;
        }
        print_r($this->getError());
        return null;
    }

}